
import request from '../../../apis/axios'
import { getStore, setStore } from '../util/store'

/**
 * 根据字典类型查询字典
 * @param {string} dictTypeCode 数据字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * */
export function getDictByType({ dictTypeCode, appCode, domainCode }) {
  const key = `${domainCode}_${appCode}_${dictTypeCode}`
  const data = getStore({ name: key })
  if (data && Array.isArray(data)) {
    return new Promise((resolve,reject)=>{
      resolve({data})
    })
  }
  return request({
    url: `/data-dict/api/v2/dictionary/${dictTypeCode}`,
    method: 'get',
    params: { appCode, domainCode }
  }).then(res => {
    const resData = res.data.sort((a, b) => { return a.sort - b.sort })
    try {
      setStore({ name: key, content: resData, type: 'session' })
    } catch (error) {
      console.log(error)
    }
    return {data:resData}
  })
}
